package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.param.setInfo.SetInfoSaveParam;
import cn.ulc.socket.admin.service.setInfo.SetInfoServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/setInfos")
public class SetInfoCtrl {
    private static final Log log = LogFactory.get();
    @Autowired
    SetInfoServ setInfoServ;
    @GetMapping("")
    public Result findDetail(){
        return Result.success(setInfoServ.findDetail());
    }
    @PostMapping("/")
    public Result save( @Validated @RequestBody SetInfoSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商户设置信息参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(setInfoServ.save(param));
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @Validated @RequestBody SetInfoSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商户设置信息参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(setInfoServ.update(id,param));
    }
}
